﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Web.Services.Protocols;
using Scan.PDAService;


namespace Scan
{
    public class WebServiceInterface
    {
        private NLog.Logger logger = NLog.LogManager.GetLogger("");

        // 获取服务器时间
        public DateTime GetServerSyncTime()
        {
            PDAService.PDAService proxy = Webservice.GetWebservice();
            return proxy.GetServerSyncTime();
        }

        // 获取用户档案
        public ResponseBaseOfSysUser GetSysUsers(Boolean Fuzzyed, string UserCode, string UserName)
        {
            PDAService.PDAService proxy = Webservice.GetWebservice();
            ResponseBaseOfSysUser response = new ResponseBaseOfSysUser();
            response.ErrorInfo = new ErrorMessage();
            response.ErrorInfo.ErrorCode = 1;
            response.ErrorInfo.ErrorMsg = "";
            try
            {
                //  response.Lists[0] = proxy.GetSgsUsers();
            }
            catch (CommunicationException cex)
            {
                response.ErrorInfo.ErrorMsg = "服务器连接失败！";
                response.ErrorInfo.ErrorCode = 9997;
                logger.ErrorException("[GetSysUsers]服务器请求失败", cex);
            }
            catch (SoapException soapEx)
            {
                response.ErrorInfo.ErrorMsg = "调用 WebService 方法出现异常！";
                response.ErrorInfo.ErrorCode = 9998;
                logger.ErrorException("[GetSysUsers]调用 WebService 方法出现异常", soapEx);
            }
            catch (Exception ex)
            {
                response.ErrorInfo.ErrorMsg = "产生其他异常！";
                response.ErrorInfo.ErrorCode = 9999;
                logger.ErrorException("[GetSysUsers]产生其他异常", ex);
            }

            return response;
        }

        // 获取职员档案
        public ResponseBaseOfPerson GetPersons(Boolean Fuzzyed, string PersonCode, string PersonName)
        {
            PDAService.PDAService proxy = Webservice.GetWebservice();
            ResponseBaseOfPerson response = new ResponseBaseOfPerson();
            response.ErrorInfo = new ErrorMessage();
            response.ErrorInfo.ErrorCode = 1;
            response.ErrorInfo.ErrorMsg = "";
            try
            {
                response = proxy.GetPersons(Fuzzyed, PersonCode, PersonName);
            }
            catch (CommunicationException cex)
            {
                response.ErrorInfo.ErrorMsg = "服务器连接失败！";
                response.ErrorInfo.ErrorCode = 9997;
                logger.ErrorException("[GetPersons]服务器请求失败", cex);
            }
            catch (SoapException soapEx)
            {
                response.ErrorInfo.ErrorMsg = "调用 WebService 方法出现异常！";
                response.ErrorInfo.ErrorCode = 9998;
                logger.ErrorException("[GetPersons]调用 WebService 方法出现异常", soapEx);
            }
            catch (Exception ex)
            {
                response.ErrorInfo.ErrorMsg = "产生其他异常！";
                response.ErrorInfo.ErrorCode = 9999;
                logger.ErrorException("[GetPersons]产生其他异常", ex);
            }

            return response;
        }

        // 获取仓库档案
        public ResponseBaseOfWarehouse GetWarehouses(Boolean Fuzzyed, string WhCode, string WhName)
        {
            PDAService.PDAService proxy = Webservice.GetWebservice();
            ResponseBaseOfWarehouse response = new ResponseBaseOfWarehouse();
            response.ErrorInfo = new ErrorMessage();
            response.ErrorInfo.ErrorCode = 1;
            response.ErrorInfo.ErrorMsg = "";
            try
            {
                response = proxy.GetWarehouses(Fuzzyed, WhCode, WhName);
            }
            catch (CommunicationException cex)
            {
                response.ErrorInfo.ErrorMsg = "服务器连接失败！";
                response.ErrorInfo.ErrorCode = 9997;
                logger.ErrorException("[GetWarehouses]服务器请求失败", cex);
            }
            catch (SoapException soapEx)
            {
                response.ErrorInfo.ErrorMsg = "调用 WebService 方法出现异常！";
                response.ErrorInfo.ErrorCode = 9998;
                logger.ErrorException("[GetWarehouses]调用 WebService 方法出现异常", soapEx);
            }
            catch (Exception ex)
            {
                response.ErrorInfo.ErrorMsg = "产生其他异常！";
                response.ErrorInfo.ErrorCode = 9999;
                logger.ErrorException("[GetWarehouses]产生其他异常", ex);
            }

            return response;
        }

        //// 获取客商档案
        //public CompanysResponse GetCompanys(Boolean Fuzzyed, int ComType, string CompCode, string CompName)
        //{
        //    PDAService.PDAService proxy = Webservice.GetWebservice();
        //    CompanysResponse response = new CompanysResponse();
        //    response.ErrorInfo = new ErrorMessage();
        //    response.ErrorInfo.ErrorCode = 1;
        //    response.ErrorInfo.ErrorMsg = "";
        //    try
        //    {
        //        response = proxy.GetCompanys(Fuzzyed, ComType, CompCode, CompName);
        //    }
        //    catch (CommunicationException cex)
        //    {
        //        response.ErrorInfo.ErrorMsg = "服务器连接失败！";
        //        response.ErrorInfo.ErrorCode = 9997;
        //        logger.ErrorException("[GetCompanys]服务器请求失败", cex);
        //    }
        //    catch (SoapException soapEx)
        //    {
        //        response.ErrorInfo.ErrorMsg = "调用 WebService 方法出现异常！";
        //        response.ErrorInfo.ErrorCode = 9998;
        //        logger.ErrorException("[GetCompanys]调用 WebService 方法出现异常", soapEx);
        //    }
        //    catch (Exception ex)
        //    {
        //        response.ErrorInfo.ErrorMsg = "产生其他异常！";
        //        response.ErrorInfo.ErrorCode = 9999;
        //        logger.ErrorException("[GetCompanys]产生其他异常", ex);
        //    }

        //    return response;
        //}

        // 获取商品档案
        public ResponseBaseOfInventory GetInventorys(Boolean Fuzzyed, string BarCode, string InvCode, string InvName)
        {
            PDAService.PDAService proxy = Webservice.GetWebservice();
            ResponseBaseOfInventory response = new ResponseBaseOfInventory();
            response.ErrorInfo = new ErrorMessage();
            response.ErrorInfo.ErrorCode = 1;
            response.ErrorInfo.ErrorMsg = "";
            try
            {
                response = proxy.GetInventorys(Fuzzyed, BarCode, InvCode, InvName);
            }
            catch (CommunicationException cex)
            {
                response.ErrorInfo.ErrorMsg = "服务器连接失败！";
                response.ErrorInfo.ErrorCode = 9997;
                logger.ErrorException("[GetInventorys]服务器请求失败", cex);
            }
            catch (SoapException soapEx)
            {
                response.ErrorInfo.ErrorMsg = "调用 WebService 方法出现异常！";
                response.ErrorInfo.ErrorCode = 9998;
                logger.ErrorException("[GetInventorys]调用 WebService 方法出现异常", soapEx);
            }
            catch (Exception ex)
            {
                response.ErrorInfo.ErrorMsg = "产生其他异常！";
                response.ErrorInfo.ErrorCode = 9999;
                logger.ErrorException("[GetInventorys]产生其他异常", ex);
            }

            return response;
        }

        // 获取单据号
        public ResponseString GetVouchCode(int VouchType)
        {
            PDAService.PDAService proxy = Webservice.GetWebservice();
            ResponseString response = new ResponseString();
            response.ErrorInfo = new ErrorMessage();
            response.ErrorInfo.ErrorCode = 1;
            response.ErrorInfo.ErrorMsg = "";
            try
            {
                response = proxy.GetVouchCode(VouchType);
            }
            catch (CommunicationException cex)
            {
                response.ErrorInfo.ErrorMsg = "服务器连接失败！";
                response.ErrorInfo.ErrorCode = 9997;
                logger.ErrorException("[GetVouchCode]服务器请求失败", cex);
            }
            catch (SoapException soapEx)
            {
                response.ErrorInfo.ErrorMsg = "调用 WebService 方法出现异常！";
                response.ErrorInfo.ErrorCode = 9998;
                logger.ErrorException("[GetVouchCode]调用 WebService 方法出现异常", soapEx);
            }
            catch (Exception ex)
            {
                response.ErrorInfo.ErrorMsg = "产生其他异常！";
                response.ErrorInfo.ErrorCode = 9999;
                logger.ErrorException("[GetVouchCode]产生其他异常", ex);
            }

            return response;
        }
        // 获取单据号
        public ResponseBaseOfComputationUnit GetComputationUnit(string UnitCode, string GroupCode)
        {
            PDAService.PDAService proxy = Webservice.GetWebservice();
            ResponseBaseOfComputationUnit response = new ResponseBaseOfComputationUnit();
            response.ErrorInfo = new ErrorMessage();
            response.ErrorInfo.ErrorCode = 1;
            response.ErrorInfo.ErrorMsg = "";
            try
            {
                response = proxy.GetComputationUnit(UnitCode, GroupCode);
            }
            catch (CommunicationException cex)
            {
                response.ErrorInfo.ErrorMsg = "服务器连接失败！";
                response.ErrorInfo.ErrorCode = 9997;
                logger.ErrorException("[GetVouchCode]服务器请求失败", cex);
            }
            catch (SoapException soapEx)
            {
                response.ErrorInfo.ErrorMsg = "调用 WebService 方法出现异常！";
                response.ErrorInfo.ErrorCode = 9998;
                logger.ErrorException("[GetVouchCode]调用 WebService 方法出现异常", soapEx);
            }
            catch (Exception ex)
            {
                response.ErrorInfo.ErrorMsg = "产生其他异常！";
                response.ErrorInfo.ErrorCode = 9999;
                logger.ErrorException("[GetVouchCode]产生其他异常", ex);
            }

            return response;
        }
    }
}
